/*
 * channeldata.cpp
 *
 *  Created on: Nov 16, 2009
 *      Author: zaca
 */

#include <biosignal/montage/channeldata.h>
#include <biosignal/montage/channel.h>
#include <biosignal/dataframe.h>

namespace biosignal {
namespace montage {

ChannelDataIterator::ChannelDataIterator(const ChannelData* channel_data, long current_id) {
    m_channel_data = channel_data;
    m_current_id = current_id;
}
bool ChannelDataIterator::operator==(const ChannelDataIterator & other) const {
    return m_current_id == other.m_current_id;
}
bool ChannelDataIterator::operator!=(const ChannelDataIterator& other) const {
    bool result = (m_current_id >= 0) ? (m_current_id != other.m_current_id)
                  : false;
    return result;
}
double ChannelDataIterator::operator*() const {
    return m_channel_data->value(m_current_id);
}
ChannelDataIterator& ChannelDataIterator::operator++() {
    if (m_current_id >= 0 && m_current_id < m_channel_data->size()-1)
    {
        m_current_id++;
    }
    else
    {
        m_current_id = -1;
    }
    return *this;

}
ChannelDataIterator& ChannelDataIterator::operator--() {
    if (m_current_id >= 0 && m_current_id < m_channel_data->size()-1)
    {
        m_current_id--;
    }
    else
    {
        m_current_id = -1;
    }
    return *this;

}
ChannelDataIterator ChannelDataIterator::operator++(int) {
    ChannelDataIterator result(*this);
    ++(*this);
    return (result);

}


ChannelData::ChannelData(const Channel* channel, const DataFrame* data_frame) :
        m_channel(channel), m_dataframe(data_frame) {

}
ChannelData::~ChannelData() {
}

const Channel * ChannelData::channel() const {
    return m_channel;
}

const DataFrame * ChannelData::data_frame() const {
    return m_dataframe;
}

ChannelDataIterator ChannelData::begin() const {
    ChannelDataIterator begin(this, 0);
    return begin;
}

ChannelDataIterator ChannelData::end() const {
    ChannelDataIterator end(this, -1);
    return end;

}

RefChannelData::RefChannelData(const RefChannel* channel, const DataFrame* data_frame) :
        ChannelData(channel, data_frame) {
    m_signal_data = data_frame->signal_source_data( channel->label() );
}
double RefChannelData::value(unsigned int sample_nr) const {
    double value = m_signal_data->value(sample_nr);
    return value;
}
int RefChannelData::size() const {
    return m_signal_data->size();
}

}
}
